<?php
/*
 * @Descripttion: 
 * @Author: 荷逸
 * @email: 563428234@qq.com
 * @Date: 2021-01-21 16:46:55
 */

namespace app\admin\controller;

use DcrPHP\File\Directory;
use DcrPHP\File\File;
use DcrPHP\File\Info;
use Exception;
use app\admin\model\FilesDirectory;
use app\admin\service\DirectoryService;
use app\admin\validate\DirectoryValidate;
use EasyZip\ZipHandle;
use Symfony\Component\Filesystem\Filesystem;
use think\Request;

class DirectoryController extends Controller
{

    // 获取文件列表
    public function index(Request $request, DirectoryService $service)
    {
        if ($request->isPost()) {
            $param = $request->param();

            $dir = isset($param['dir']) && !empty($param['dir']) ? urldecode($param['dir']) : str_replace('\\', '/', app()->getRootPath());
            if (isset($param['dir']) && !empty($param['dir'])) {
                $dir = urldecode($param['dir']);
            } else {
                $rootDir = str_replace('\\', '/', app()->getRootPath());
                $dir = substr($rootDir, 0, strlen($rootDir) - 1);
            }

            if (isset($param['all'])  &&  $param['all'] == 'true') {
                $service->getSearch($dir, $param);
            } else {
                $service->getList($dir, $param);
            }


            $data = $service->decompose();

            return success('成功', '', ['data' => $data, 'path' => $dir]);
        }

        return $this->fetch();
    }
    public function zipFile(Request $request)
    {
        $param = $request->param();
        if ($request->isPost()) {
            $file = $param['file'];
            // $fileArr = explode('/',$param['file']);
            // $end = end($fileArr);
            // $dir_name=str_replace($end,'',$file);

            $dir_name = $file; // 需要压缩的目录或文件路径
            $zip_name = $file . '.zip'; // 压缩文件保存路径

            $result =  ZipHandle::zip($dir_name, $zip_name);
            return empty($result) ? success('操作成功', URL_CLOSE_REFRESH) : error('压缩失败');
        }

        $this->assign([
            'data' => $param
        ]);
        return $this->fetch();
    }
    public function getDirSize(Request $request, DirectoryValidate $validate)
    {
        $param = $request->param();
        $validate_result = $validate->scene('get')->check($param);
        if (!$validate_result) {
            return error($validate->getError());
        }

        return success('获取成功', '', getSize(dirsize($param['dir'])));
    }
    public function delFile(Request $request, Filesystem $system)
    {
        $param = $request->param();
        $result = $system->remove($param['file']);
        return $result == null ? success('删除成功', URL_RELOAD) : error('删除失败');
    }
    public function rename(Request $request, Filesystem $system)
    {
        $param = $request->param();

        if ($request->isPost()) {
            $target = $param['path'] . '/' . $param['name'];
            $result = $system->rename($param['file'], $target);
            return $result == null ? success('重命名成功', URL_CLOSE_REFRESH) : error('重命名失败');
        }
        $this->assign([
            'data' => $param
        ]);
        return $this->fetch();
    }
    //检测重复文件
    public function CheckExistsFiles(Request $request, DirectoryService $service, DirectoryValidate $validate)
    {
        $param = $request->param();
        $validate_result = $validate->scene('check')->check($param);
        if (!$validate_result) {
            return error($validate->getError());
        }
        $directory = new FilesDirectory($param['dir']);
        $data = $directory->list();
        foreach ($data as $vo) {
            if ($vo['base_name'] == $param['filename'] && $vo['type'] == $param['filetype']) {
                $info[] = $vo;
            }
        }

        if (!isset($info)) {
            $info = [];
        }
        return success('获取成功', '', $info);
    }
    // 粘贴拷贝文件
    public function CopyFile(Request $request, Filesystem $system)
    {
        $param = $request->param();

        if ($request->isPost()) {
            if (is_dir($param['sfile'])) {
                $result = $system->mirror($param['sfile'], $param['dfile']);
                return $result == null ? success('目录复制成功') : error('目录复制失败');
            } else {
                $result = $system->copy($param['sfile'], $param['dfile']);
                return $result == null ? success('文件复制成功') : error('文件复制失败');
            }
        }
    }
    // 移动文件
    public function MvFile(Request $request, Filesystem $system)
    {
        $param = $request->param();
        // dump($param);
        // exit;
        if ($request->isPost()) {
            if (is_dir($param['sfile'])) {
                $result = $system->mirror($param['sfile'], $param['dfile']);
                if($result == null){
                    // 复制目录成功
                    $res = $system->remove($param['sfile']);
                    return $res == null ? success('移动目录成功') : error('移动目录失败');

                }else{
                    // 复制目录失败
                    return error('移动目录失败');
                }
            } else {
                $result = $system->copy($param['sfile'], $param['dfile']);
                if($result == null){
                    // 复制文件成功
                    $res = $system->remove($param['sfile']);
                    return $res == null ? success('移动文件成功') : error('移动文件失败');
                }else{
                    // 复制文件失败
                    return error('移动文件失败');
                }

            }

        }
    }
}
